import { _decorator, Component, Node, Sprite, Camera, Vec3, v3 } from 'cc';
import { BasicComponet } from '../../Init/Basic/BasicComponet';
import { GlobalTmpData } from '../../Init/Config/GlobalTmpData';
import GlobalPool from '../../Init/Tools/GlobalPool';
const { ccclass, property } = _decorator;

@ccclass('armyBulletCd')
export class armyBulletCd extends BasicComponet {
    _sp: Sprite = null;
    _camera: Camera = null;
    cd = 0;
    curt = 0;
    p = v3();

    setData(d: { parent: Node, camera: Camera, cd: number, p: Vec3 }) {
        this.node.parent = d.parent;
        this.curt = 0;
        this.cd = d.cd;
        this._camera = d.camera;
        this.p.set(d.p);
        this._sp = this.node.getComponent(Sprite);
        this._sp.fillRange = 1;
        this.calPos();
    }

    update(dt) {
        if (GlobalTmpData.Game.isGamePause) return;
        this.curt += dt;
        this.calPos();
        if (this.curt >= this.cd) {
            this.curt = this.cd;
            GlobalPool.put(this.node);
        }
        this._sp.fillRange = 1 - this.curt / this.cd;
    }
    _tmpPos = v3();
    calPos() {
        this._camera.convertToUINode(this.p, this.node.parent, this._tmpPos);
        this._tmpPos.y += 90;
        this.node.setPosition(this._tmpPos);
    }
}

